<template>
    <x-search-bar class="mb-8-2">
        <template #default="{ gutter, colSpan }">
            <a-input-search
                v-model:value="phone"
                placeholder="请输入查询的手机号"
                enter-button
                @search="handleSearch"
            />
        </template>
    </x-search-bar>
    <a-row
        :gutter="8"
        :wrap="false">
        <a-col flex="auto">
            <a-card type="flex">
                <a-table
                    :columns="columns"
                    :data-source="listData"
                    :loading="loading"
                    :pagination="paginationState"
                    :scroll="{ x: 1000 }"
                    @change="onTableChange">
                    <template #bodyCell="{ column, record }">
                        <template v-if="'ip' === column.key">
                            <a @click="handleIpClick(record.ip)">{{ record.ip }}</a>
                        </template>
                        <template v-if="'deviceId' === column.key">
                            <a @click="handleDeviceIdClick(record.deviceId)">{{ record.deviceId }}</a>
                        </template>
                    </template>
                </a-table>
            </a-card>
        </a-col>
    </a-row>

    <a-modal
        v-model:visible="isModalVisible"
        title="详细信息"
        @cancel="handleCancel"
        @ok="handleOk"
        width="60%"
    >
        <a-table
            :columns="modalColumns"
            :data-source="modalData"
            :pagination="false"
            :scroll="{ y: 300 }"
        />
    </a-modal>
</template>

<script setup>
import apis from '@/apis'
import { formatUtcDateTime } from '@/utils/util'
import { config } from '@/config'
import { usePagination } from '@/hooks'
import { ref } from 'vue'

const columns = [
    { title: '用户名', dataIndex: 'userName',key: 'userName' },
    { title: '手机号', dataIndex: 'phone',key: 'phone' },
    { title: '地址', dataIndex: 'address',key: 'address' },
    { title: 'IP', dataIndex: 'ip' ,key: 'ip' },
    { title: '设备ID', dataIndex: 'deviceId' ,key: 'deviceId' },
    { title: '设备类型', dataIndex: 'deviceType', key: 'deviceType' },
    { title: '设备品牌', dataIndex: 'deviceBrand' ,key: 'deviceBrand' },
    { title: '设备型号', dataIndex: 'deviceModel', key: 'deviceModel' },
    { title: '系统名称', dataIndex: 'osName' ,key: 'osName' },
    { title: '系统版本', dataIndex: 'osVersion',key: 'osVersion' },
    { title: '系统语言', dataIndex: 'osLanguage', key: 'osLanguage' },
    { title: '登录时间', dataIndex: 'created_at', key: 'created_at' },
]
const phone = ref()
const { listData, loading, showLoading, hideLoading, paginationState, resetPagination, searchFormData } =
    usePagination()
const isModalVisible = ref(false)
const modalData = ref([]) // Modal 显示的数据
const modalColumns = [
    { title: '用户名', dataIndex: 'userName',key: 'userName' },
    { title: '手机号', dataIndex: 'phone',key: 'phone' },
    { title: '地址', dataIndex: 'address',key: 'address' },
    { title: 'IP', dataIndex: 'ip' ,key: 'ip' },
    { title: '设备ID', dataIndex: 'deviceId' ,key: 'deviceId' },
    { title: '登录时间', dataIndex: 'created_at', key: 'created_at' },
]

/**
 * 获取用户列表
 * @returns {Promise<void>}
 */
async function getPageList() {
    try {
        if (phone.value == ""){
            return
        }
        showLoading()

        const { pageSize, current } = paginationState

        const { success, data, total } = await apis.users
            .getUsersInfoList({
                pageSize,
                page: current,
                phone:phone.value,
            })
            .catch(() => {
                throw new Error()
            })
        hideLoading()
        if (config('http.code.success') === success) {
            listData.value = data
            paginationState.total = total
        }
    } catch (error) {
        hideLoading()
    }
}
/**
 * 分页
 */
function onTableChange({ current, pageSize }) {
    paginationState.current = current
    paginationState.pageSize = pageSize
    getPageList()
}

/**
 * 搜索
 */
function handleSearch() {
    resetPagination()
    getPageList()
}
/**
 * 重置
 */
async function handleIpClick(ip) {
    console.log('IP clicked:', ip)
    try {
        showLoading()
        // 请求 IP 相关数据
        const { success, data } = await apis.users.getUsersInfoListByIpOrMac({ip: ip })
        hideLoading()
        if (config('http.code.success') === success) {
            modalData.value = data
            isModalVisible.value = true
        }
    } catch (error) {
        hideLoading()
    }
}

// 点击 设备ID
async function handleDeviceIdClick(deviceId) {
    console.log('Device ID clicked:', deviceId)
    try {
        showLoading()
        // 请求设备ID相关数据
        const { success, data } = await apis.users.getUsersInfoListByIpOrMac({ deviceId:deviceId })
        hideLoading()
        if (config('http.code.success') === success) {
            modalData.value = data
            isModalVisible.value = true
        }
    } catch (error) {
        hideLoading()
    }
}

// 关闭弹窗
function handleCancel() {
    isModalVisible.value = false
}

// 确认
function handleOk() {
    isModalVisible.value = false
}

</script>

<style lang="less" scoped></style>
